/*
 * $Id: CleanupArchivalMessageConverter.java,v 1.2 2009/07/24 18:28:02 oliver Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

/**
 * 
 */
package com.ge.healthcare.autosc.common.message.converter;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.ActRequestList;

/**
 * @author 212042764
 * 
 */
public class CleanupArchivalMessageConverter implements MessageConverter {

	/**
	 * Spring likes me
	 */
	public CleanupArchivalMessageConverter() {
		super();
	}

	/**
	 * @see org.springframework.jms.support.converter.MessageConverter#fromMessage(javax.jms.Message)
	 */
	public Object fromMessage(Message message) throws JMSException,
			MessageConversionException {
		String methodName = "fromMessage";
		if (!(message instanceof MapMessage)) {
			throw new MessageConversionException(
					"Message isn't map message, message:" + message);
		}
		Long requestListId = ((MapMessage) message)
				.getLongProperty(ActRequestList.COL_REQUEST_ID);
		ASCLogger.debug(this.getClass(), methodName,
				"Received Request List Id:" + requestListId);
		if (ASCUtil.isNullOrEmptyObject(requestListId)) {
			throw new MessageConversionException(
					"Request list id not specified in message:" + message);
		}
		return requestListId;
	}

	/**
	 * @see org.springframework.jms.support.converter.MessageConverter#toMessage(java.lang.Object,
	 *      javax.jms.Session)
	 */
	public Message toMessage(Object object, Session session)
			throws JMSException, MessageConversionException {
		if (!(object instanceof Long)) {
			throw new MessageConversionException(
					"Object isn't an instance of Long, object:" + object);
		}
		MapMessage message = session.createMapMessage();
		// Setting request list id
		message.setLongProperty(ActRequestList.COL_REQUEST_ID, (Long) object);
		return message;
	}

}
